package comframe.jdbc.sql.support;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import comframe.jdbc.sql.condition.Condition;
import comframe.jdbc.sql.datasource.Config;
import comframe.jdbc.sql.datasource.RayDataSource;
import comframe.jdbc.sql.support.struct.RM;
import comframe.jdbc.sql.support.struct.TableStruct;

/**
 * 数据库查询模板
 * 
 * @author ray
 * 
 */
public class SqlTemplete implements Templete {
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SqlTemplete.class);
	/** 查询工厂 */
	private ExFacotry factory;
	/**
	 * 表结构对象
	 * 
	 * 中实现了 把对象转成 jdbc 查询需要的数据 主要有 sql， 参数，
	 * */
	private TableStruct tableStruct = new TableStruct();

	public SqlTemplete(ExFacotry factory) {
		this.factory = factory;
	}

	public SqlTemplete(Config config) {
		this.factory = new ExFacotry(config);
	}

	public SqlTemplete(RayDataSource dataSource) {
		this.factory = new ExFacotry(dataSource);
	}

	public SqlTemplete(Config config,boolean IsSingleton) {
		this.factory = new ExFacotry(config,IsSingleton);
	}

	public SqlTemplete(RayDataSource dataSource,boolean IsSingleton) {
		this.factory = new ExFacotry(dataSource,IsSingleton);
	}


	public boolean save(Object object) throws Exception {
		return factory.getEx().insert(tableStruct.insertSQL(object));
	}
	
	
	public boolean saveBatch(List<?> object) throws Exception {
		return factory.getEx().insertBatch(tableStruct.insertBatchSQL(object));
	}
	

	public boolean delete(Object object) throws Exception {
		return factory.getEx().delete(tableStruct.deleteSQL(object));
	}
	
	public boolean deleteById(Class<?>  clazz,Object id) throws Exception {
		return factory.getEx().delete(tableStruct.deleteSQLbyId(clazz,id));
	}

	public boolean delete(Condition condition, Class<?> clazz)
			throws Exception {
		return factory.getEx().delete(tableStruct.deleteSQL(condition, clazz));
	}

	public List<Object> find(Class<?> clazz) throws Exception {
		return factory.getEx().find(tableStruct.findSQL(clazz));
	}
    
	public Object findById(Class<?> clazz, Object value) throws Exception {
		List<Object> list = factory.getEx().find(tableStruct.findIdSQL(clazz,value));
		if(list != null && list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public List<Object> find(Condition condition, Class<?> clazz)
			throws Exception {
		return factory.getEx().find(tableStruct.findSQL(condition, clazz));
	}

	public boolean update(Object object) throws Exception {
		return factory.getEx().update(tableStruct.updateSQL(object));
	}

	public boolean updateIsExist(Object object) throws Exception {
		return factory.getEx().update(tableStruct.updateExistSQL(object));
	}

	public boolean update(Object object, Condition condition)
			throws Exception {
		return factory.getEx().update(tableStruct.updateSQL(object,condition));
	}

	public boolean updateIsExist(Object object, Condition condition)
			throws Exception {
		return factory.getEx().update(tableStruct.updateExistSQL(object,condition));
	}

	public boolean updateBySql(String sql,Class<?> clazz) throws Exception{
		 return factory.getEx().update(tableStruct.updateExecuteSql(sql,clazz));
	}
	
	public Integer count(Class<?> clazz) throws Exception {
		return factory.getEx().count(tableStruct.countSql(clazz));
	}

	public Integer count(Condition conditionSql, Class<?> clazz)
			throws Exception {
		return factory.getEx().count(tableStruct.countSql(conditionSql, clazz));
	}
	
	public Integer count(String name, Object value, Class<?> clazz)
			throws Exception {
		return factory.getEx().count(tableStruct.countSql( name,  value, clazz));
	}
	
	public Integer count(String sql)
			throws Exception {
		return factory.getEx().count(tableStruct.countSql(sql));
	}

	public List<Object> PageQuery(int page, int pageSize,String sql, Class<?> clazz)
			throws Exception {
		RM rm = new RM();
		if (DataType.MSSQL.equals(factory.getDataType().toLowerCase())) {
			
		} else if (DataType.MYSQL.equals(factory.getDataType().toLowerCase())) {

		} else if (DataType.ORACLE.equals(factory.getDataType().toLowerCase())) {
			rm = tableStruct.pageSqlForOracle(page, pageSize,sql,
					clazz);
		} else {
			logger.error("现在框架不支持该类型[" + factory.getDataType() + "]的数据库");
		}
		return factory.getEx().pageQuery(rm);
	}
	
	public List<Object> PageQuery(int page, int pageSize, Class<?> clazz)
			throws Exception {
		RM rm = new RM();
		if (DataType.MSSQL.equals(factory.getDataType().toLowerCase())) {
			rm = tableStruct.pageSqlForMsSql(page, pageSize, clazz);
		} else if (DataType.MYSQL.equals(factory.getDataType().toLowerCase())) {

		} else if (DataType.ORACLE.equals(factory.getDataType().toLowerCase())) {
			rm = tableStruct.pageSqlForOracle(page, pageSize,
					clazz);
		} else {
			logger.error("现在框架不支持该类型[" + factory.getDataType() + "]的数据库");
		}
		return factory.getEx().pageQuery(rm);
	}

	public List<Object> PageQuery(int page, int pageSize,
			Condition conditionSql, Class<?> clazz) throws Exception {
		RM rm = new RM();
		if (DataType.MSSQL.equals(factory.getDataType().toLowerCase())) {
			rm = tableStruct.pageSqlForMsSql(page, pageSize, conditionSql,
					clazz);
		} else if (DataType.MYSQL.equals(factory.getDataType().toLowerCase())) {

		} else if (DataType.ORACLE.equals(factory.getDataType().toLowerCase())) {
			rm = tableStruct.pageSqlForOracle(page, pageSize, conditionSql,
					clazz);
		} else {
			logger.error("现在框架不支持该类型[" + factory.getDataType() + "]的数据库");
		}
		return factory.getEx().pageQuery(rm);
	}

	public boolean deleteAll(Class<?> clazz) throws Exception {
		return factory.getEx().delete(tableStruct.deleteAllSQL(clazz));
	}

	public List<Object> querySql(String sql, Class<?> clazz)
			throws Exception {
		return factory.getEx().find(tableStruct.findQuerySQL(sql,clazz));
	}

	public Connection createConnection()
	throws SQLException, ClassNotFoundException {
       return factory.getConn();
}

	

}
